package com.photography.config;

import com.photography.common.JwtInterceptor;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {
    
    @Resource
    private JwtInterceptor jwtInterceptor;

    @Value("${file.upload.path:/file}")
    private String uploadBasePath;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        // 配置文件访问映射
        String uploadPath = System.getProperty("user.dir") + uploadBasePath;
        registry.addResourceHandler("/api/files/**")
                .addResourceLocations("file:" + uploadPath + "/");
    }

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        // 指定controller统一的接口前缀
        configurer.addPathPrefix("/api", clazz -> clazz.isAnnotationPresent(RestController.class));
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(jwtInterceptor)
                .addPathPatterns("/**")  // 拦截所有请求
                .excludePathPatterns(
                        "/api/user/login",     // 用户登录接口
                        "/api/user/files/**",   //头像接口
                        "/api/photographers/login", // 摄影师登录接口
                        "/api/user/register",  // 用户注册接口
                        "/api/photographer/register", // 摄影师注册接口
                        "/api/photographer/ex", // 摄影师专业领域枚举接口
                        "/api/product/*/static/**",    // 静态资源
                        "/api/files/**",     // 文件访问
                        "/api/common/**",       // 公共接口
                        "/api/product/**", // 产品详情查询接口
                        "/api/product/search",  // 产品搜索接口
                        "/api/product/page",    // 产品分页查询接口
                        "/api/category/**",    // 分类分页查询接口
                        "/api/category/withProducts",    // 分类及其产品查询接口
                        "/api/review/page",//评论查询
                        "/api/review/product/**",//评论查询
                        "/api/carousel/page",//轮播图查询
                        "/api/carousel/**",//轮播图文件
                        "/api/alipay/notify",
                        "/api/alipay/create",
                        "/api/alipay/query"
                );
    }
} 